03. Exercise: A Basic Notification
L1 A03 A Basic Notifcation V2
Android Developer Documentation
Exercise
- Open the
NotificationUtils.ktclass and find Step 1.1. You will be implementing an extension function toNotificationManagerto send notifications without rewriting the necessary code. The emptysendNotification()function is already given and has TODOs where you will add the implementation.
// TODO: Step 1.1 extension function to send messages (GIVEN)
/**
* Builds and delivers a notification.
*
* @param messageBody, notification text.
* @param context, activity context.
*/
fun NotificationManager.sendNotification(messageBody: String, applicationContext: Context) {
- To support devices running older versions you need to use
NotificationCompatbuilder instead of notification builder. Get an instance of theNotificationCompatbuilder, pass in the app context and a channel id. The channel id is a string value from string resources which uses the matching channel. Starting with API level 26, all notifications must be assigned to a channel. We're going to go into channels in much more detail later - for now, just use the string channel id.
// TODO: Step 1.2 get an instance of NotificationCompat.Builder
val builder = NotificationCompat.Builder(
applicationContext,
applicationContext.getString(R.string.egg_notification_channel_id)
)
- Set the notification icon to represent your app, title and the content text for the message you want to give to the user. You'll see more options to customize your notification further in the lesson but this is the minimum amount of data you need to set in order to send a notification.
// TODO: Step 1.3 set title, text and icon to builder
.setSmallIcon(R.drawable.cooked_egg)
.setContentTitle(applicationContext
.getString(R.string.notification_title))
.setContentText(messageBody)
- Next, you need to call
notify()with a unique id for your notification and with the Notification object from your builder. This id represents the current notification instance and is needed for updating or canceling this notification. Since your app will only have one active notification at a given time, you can use the same id for all your notifications. You are already given a constant for this purpose calledNOTIFICATION_IDinNotificationUtils.kt. Notice we can directly callnotify()since you are performing the call from an extension function on the same class.
// TODO: Step 1.4 call notify to send the notification
// Deliver the notification
notify(NOTIFICATION_ID, builder.build())
Now that we've written all the code we need to create and trigger a notification in this extension function - let's call it. Open
EggTimerViewModel.ktand find thestartTimer()function. This function creates an alarm with the selected time interval when the user enables your egg timer.Next let’s trigger a notification in this function when the user starts the timer. In order to call the
sendNotification()function you previously implemented, you need an instance ofNotificationManager.NotificationManageris a system service which provides all the functions exposed for notifications api, including the extension function you added. Anytime you want to send, cancel or update a notification you need to request an instance of theNotificationManagerfrom the system. Call thesendNotification()extension function with the notification message and with the context.
// EggTimerViewModel.kt
// TODO: Step 1.5 get an instance of NotificationManager
// and call sendNotification
val notificationManager = ContextCompat.getSystemService(
app,
NotificationManager::class.java
) as NotificationManager
notificationManager.sendNotification(app.getString(R.string.timer_running), app)
If you open logcatand search for “No Channel found”, you should see an error message that the egg_channel, you are trying to use, does not exist. In the following steps you will learn more about the Notification Channels and how to create one.